package com.zzyl.nursing.service.impl;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.MemberProjectPageDto;
import com.zzyl.nursing.vo.CountAndTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 预约信息Service业务层处理
 * 
 * @author alexis
 * @date 2025-01-10
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {   reservation.setStatus(0);
        reservation.setCreateBy(String.valueOf(UserThreadLocal.getUserId()));
        reservation.setUpdateBy(String.valueOf(UserThreadLocal.getUserId()));
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteById(id);
    }

    @Override
    public Integer cancellCount() {
        Long time =System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Long userId = UserThreadLocal.getUserId();
        Integer i = reservationMapper.getCancellCount(startTime,endTime,userId);

        return i;
    }

    @Override
    public CountAndTimeVo countByTime( Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        CountAndTimeVo countAndTimeVo =  reservationMapper.countByTime(startTime,endTime);
        return countAndTimeVo;
    }

    @Override
    public TableDataInfo<Reservation> pageAll(MemberProjectPageDto memberProjectPageDto) {
        Page<Reservation> page = new Page<>(memberProjectPageDto.getPageNum(), memberProjectPageDto.getPageSize());
        page = page(page, Wrappers.lambdaQuery(Reservation.class)
                .eq(memberProjectPageDto.getStatus()!=null,Reservation::getStatus, memberProjectPageDto.getStatus())
                .eq(memberProjectPageDto.getName()!=null&&!memberProjectPageDto.getName().isEmpty(),Reservation::getName, memberProjectPageDto.getName()));
        TableDataInfo<Reservation> tableDataInfo = getTableDataInfo(page);
        return tableDataInfo;
    }

    private TableDataInfo<Reservation> getTableDataInfo(Page<Reservation> page) {
        TableDataInfo<Reservation> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    @Override
    public void cancel(Long id) {
        Reservation reservation = reservationMapper.selectOne(Wrappers.lambdaQuery(Reservation.class).eq(Reservation::getId, id).eq(Reservation::getStatus,0));
        if(ObjectUtil.isEmpty(reservation)){
            throw new BaseException("预约信息不存在");
        }
        reservation.setStatus(2);
        reservation.setUpdateBy(String.valueOf(UserThreadLocal.getUserId()));
        updateReservation(reservation);
    }

    @Override
    public void CancelJob() {

        List<Reservation> reservations = list(Wrappers.lambdaQuery(Reservation.class).eq(Reservation::getStatus, 0)
                .lt(Reservation::getTime, LocalDateTime.now().minusMinutes(30)));

        reservations.forEach(reservation -> {
            reservation.setStatus(3);
        });
        updateBatchById(reservations);
    }
}
